package firstping.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import firstping.data.QnAPersistenceManager;
import firstping.model.Domain;

@SuppressWarnings("serial")
public class DeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html");
		resp.getWriter().println("Hello, world");
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		String name = req.getParameter("name");
		name=URLDecoder.decode(name,"UTF-8");
		
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		
		if(customer != null)
		{
			Query query = psm.newQuery(Domain.class);
			query.setFilter("name=='"+name+"' && email=='"+customer+"'");
			@SuppressWarnings("unchecked")
			List<Domain> check = (List<Domain>) query.execute();
			if(check.size()>0)
			{
				psm.deletePersistent(check.get(0));
			}
		} else {
			resp.sendRedirect("/");
		}
		resp.sendRedirect("/account");
	}

}
